package com.example.huizhou.controller;

import com.example.huizhou.Service.CourseService;
import com.example.huizhou.Service.StudentService;
import com.example.huizhou.Service.TeacherService;
import com.example.huizhou.bean.Course;
import com.example.huizhou.bean.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Resource
    CourseService courseService;
    @Resource
    TeacherService teacherService;
    @Resource
    StudentService studentService;

    @RequestMapping("/mycourse")
    public String mycourse()
    {
        return "mycourse";
    }
    //检索功能
    @RequestMapping("/imycourse")
    @ResponseBody
    public Object imycourse(String term,HttpServletRequest request)
    {
        HttpSession session=request.getSession(false);
        String userid = session.getAttribute("user_id").toString();
        List<Course> infolist=courseService.findscourse(term,userid);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("infoList",infolist);
        return map;
    }

    @RequestMapping("/reelection")
    public String reelection()
    {
        return "reelection";
    }
    //退选功能
    @RequestMapping("/ireelection")
    @ResponseBody
    public Object ireelection(String term,HttpServletRequest request)
    {
        HttpSession session=request.getSession(false);
        String userid = session.getAttribute("user_id").toString();
        List<Course> infolist=courseService.findscourse(term,userid);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("infoList",infolist);
        return map;
    }

    @RequestMapping("/election")
    public String election()
    {
        return "election";
    }

    @RequestMapping("/ielection")
    @ResponseBody
    public Object ielection(String term)
    {
        List<Course> infolist=courseService.findcourse(term);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("infoList",infolist);
        return map;
    }
//选课功能
    @RequestMapping("xuanke")
    @ResponseBody
    public String xuanke(String courseid,HttpServletRequest request)
    {
        HttpSession session=request.getSession(false);
        String userid = session.getAttribute("user_id").toString();
        List<Course> infolist=courseService.findocourse(userid,courseid);
        System.out.print(infolist.size());
        if(infolist.size()==0)
        {
            courseService.insert(userid,courseid);
            return "成功选：";
        }
        return "不能重复选：";
    }

    @RequestMapping("tuike")
    @ResponseBody
    public String tuike(String courseid,HttpServletRequest request)
    {
        HttpSession session=request.getSession(false);
        String userid = session.getAttribute("user_id").toString();
        courseService.delete(userid,courseid);
        return null;
    }

    @RequestMapping("year")
    @ResponseBody
    public String year(HttpServletRequest request)
    {
        HttpSession session=request.getSession(false);
        String userid = session.getAttribute("user_id").toString();
        String n=courseService.year(userid);
        Integer m = 2*(2022-Integer.valueOf(n));
        n=String.valueOf(m);
        return n;
    }

    //登录
    @RequestMapping("/login")
    public String login(String identity, String userid, String password){
        return "login";
    }

    @RequestMapping(value = "/mlogin",method = RequestMethod.GET)
    @ResponseBody
    public String mlogin(String identity, String userid, String password,HttpServletRequest request) throws IOException {
        HttpSession session = request.getSession();
        session.setAttribute("user_id",userid);
        if(identity.equals("教师")) {
            String psw ;
            int id = Integer.valueOf(userid);
            psw=teacherService.findtc(id);
            if(psw==null){
                return "null";
            }else if(psw.equals(password)){
                return "/teacher/homepage_tc";
            }else{
                return "error";
            }
        }else{
            String psw = studentService.findsd(userid);
            System.out.println(psw);
            if(psw==null){
                return "null";
            }else if(psw.equals(password)){
                return "/student/homepage_sd";
            }else{
                return "error";
            }
        }

    }

    @RequestMapping("/homepage_sd")
    public String homepage_sd(HttpServletRequest request){
        HttpSession session=request.getSession(false);
        return "homepage_sd";
    }
    //个人信息
    @RequestMapping("/学生个人信息")
    public String msg(HttpServletRequest request, Model model){
        HttpSession session=request.getSession(false);
        String userid = session.getAttribute("user_id").toString();
        Student student = studentService.findxs(userid);
        model.addAttribute("student",student);
        return "msg";
    }
    //修改密码
    @RequestMapping("/修改密码")
    @ResponseBody
    public String newpsw(String psw,String psw1,HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(psw.equals("")){
            return "psw";
        }else if(psw1.equals("")){
            return "psw1";
        }else if(!psw.equals(psw1)){
            return "unequal";
        }else{
            String id = session.getAttribute("user_id").toString();
            studentService.updatepsw(id,psw);
            return "yes";
        }
    }
}
